<template>
    <el-form label-width="80px" :rules="rules" :model="goodsAdd" ref="goodsAdd">
        <el-form-item label="商品名" prop="name">
            <el-input placeholder="请输入商品名" v-model="goodsAdd.name"></el-input>
        </el-form-item>
        <el-form-item label="商品分类" prop="category">
            <el-select v-model="goodsAdd.category">
                <el-option v-for="item in goodsTypes" :key="item.id" :value="item.cateName"
                    :label="item.cateName"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
            <el-input-number v-model="goodsAdd.price"></el-input-number>
        </el-form-item>
        <el-form-item label="商品图片" prop="imgUrl">
            <el-upload class="avatar-uploader " :action="BASE_URL + '/goods/goods_img_upload'" :on-success="imgSuccess"
                :show-file-list="false">
                <img v-if="goodsAdd.imgUrl" :src="BASE_URL + '/upload/imgs/goods_img/' + goodsAdd.imgUrl"
                    class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>

        </el-form-item>
        <el-form-item label="商品描述" prop="goodsDesc">
            <el-input type="textarea" :rows="0" v-model="goodsAdd.goodsDesc">
            </el-input>
        </el-form-item>
        <el-form-item>
            <!-- 按钮根据父组件给的值进行渲染 -->
            <el-button type="primary" @click="add">{{ type === 'add' ? '添加' : '编辑' }}商品</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
import { goodsAddReq, goodsTypeReq } from '@/api/goods.api';
import { BASE_URL } from '@/utils/common';
export default {
    props: {
        // add（添加操作）   edit（编辑操作）
        type: {
            type: String,
            default: 'add'
        },
        // 父组件传过来的数据
        defaultForm: {
            type: Object,
            // 默认值一个空对象
            default: () => ({})
        }

    },
    // 监听父组件传的数据是否发生变化，变化之后执行一次
    watch: {
        defaultForm: {
            handler: function () {
                // 当type=edit时，将父组件传过来的数据赋值给goodsAdd
                if (this.type === 'edit') {
                    this.goodsAdd = { ...this.defaultForm }
                    // 图片处理
                    this.goodsAdd.imgUrl = this.goodsAdd.imgUrl.split('/').pop()
                }
            },
            // 立即执行一次
            immediate: true
        }
    },
    data() {
        return {
            BASE_URL,
            // 商品类型
            goodsTypes: [],
            // 请求添加商品的接口
            goodsAdd: {
                name: '',
                category: '',
                price: '',
                imgUrl: '',
                goodsDesc: ''
            },
            // 表单校验
            rules: {
                name: { required: true, message: '请输入商品名', tirgger: 'blur' },
                category: { required: true, message: '请输入商品分类', tirgger: 'change' },
                price: { type: 'number', required: true, message: '请输入商品价格', tirgger: 'change', },
                imgUrl: { required: true, message: '请上传商品图片', tirgger: 'change' },
                goodsDesc: { required: true, message: '请输入商品描述', tirgger: 'blur' },
            }
        };
    },

    async created() {
        // 获取商品分类
        const res = await goodsTypeReq(this.params)
        this.goodsTypes = res.data.data
    },

    methods: {
        // 图片上传
        imgSuccess(response) {
            // this.imgUrl = response.imgUrl
            let imgUrls = response.imgUrl
            this.goodsAdd.imgUrl = imgUrls.split('/').pop()
            this.$refs.goodsAdd.validateField('imgUrl')
        },

        // 添加商品
        add() {
            this.$refs.goodsAdd.validate(async res => {
                console.log(res);
                if (res) {
                    // 子传父
                    this.$emit('success', this.goodsAdd)
                    this.$emit('edit', this.goodsAdd)
                }
            })
        }
    },

};
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
    cursor: pointer;

    position: relative;
    overflow: hidden;
}

.el-upload i {
    border-radius: 6px;

    border: 1px dashed rgb(217, 217, 217)
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.el-input {
    width: 320px;
}

/deep/ .el-textarea__inner {
    width: 320px;
}
</style>